package com.library.bookmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.bookmanagement.entity.Book;
import com.library.bookmanagement.mapper.BookMapper;
import com.library.bookmanagement.mapper.BookSituationMapper;
import com.library.bookmanagement.mapper.ReserveMapper;
import com.library.bookmanagement.service.BookService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/8 16:52
 * @description：书籍service接口实现类
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookSituationMapper bookSituationMapper;
    @Autowired
    private ReserveMapper reserveMapper;

    /**
    * @Description: 显示图书列表
    * @Param: [pageNum, pageSize]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Map<String, Object>> getBooksList(Integer pageNum, Integer pageSize) {
        String orderBy = "borrow_count desc";
        PageHelper.startPage(pageNum,pageSize,orderBy);
        Page<Book> books = bookMapper.selectBookList();
        if(books.getTotal() != 0){
            HashMap<String,Object> map = new HashMap<>();
            map.put(Constant.MESSAGE_TOTALNUM,books.getTotal());
            map.put(Constant.MEASSAGE_DATAS,books);
            return ResultUtil.ok().data(map);
        }
        return ResultUtil.error().message("查询结果返回错误");
    }

    /**
    * @Description: 按照类型显示图书
    * @Param: [pageNum, pageSize, bookType]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Map<String, Object>> getBookTypeList(Integer pageNum, Integer pageSize,String bookType) {
        String orderBy = "borrow_count desc";
        Integer typeId = bookSituationMapper.selectPrimaryKeyByTypeName(bookType);
        PageHelper.startPage(pageNum,pageSize,orderBy);
        Page<Book> books = bookMapper.selectByType(typeId);
        if(books.getTotal() != 0){
            HashMap<String,Object> map = new HashMap<>();
            map.put(Constant.MESSAGE_TOTALNUM,books.getTotal());
            map.put(Constant.MEASSAGE_DATAS,books);
            return ResultUtil.ok().data(map);
        }
        return ResultUtil.error().message("查询结果返回错误");
    }

    /**
    * @Description: 根据图书和类型显示图书
    * @Param: [pageNum, bookName, bookType]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Map<String, Object>> queryBookByTypeAndName(String bookName, String bookType) {
        Integer typeId = bookSituationMapper.selectPrimaryKeyByTypeName(bookType);
        List<Book> books = bookMapper.selectByTypeAndName(typeId,bookName);
        if(books.size() != 0){
            return ResultUtil.ok().data(books);
        }
        return ResultUtil.error().message("查询结果返回错误");
    }

}
